import 'package:flutter/material.dart';
import 'package:shihui/common/component_index.dart';
import 'package:shihui/resources/iconfont.dart';
import 'package:shihui/ui/pages/page_exchange.dart';
import 'package:shihui/ui/pages/page_home.dart';
import 'package:shihui/ui/pages/page_index.dart';
import 'package:shihui/ui/widgets/bottom_tab_bar.dart';

class MainPage extends StatefulWidget {
  @override
  _MainState createState() {
    // TODO: implement createState
    return _MainState();
  }
}

class _MainState extends State<MainPage> with SingleTickerProviderStateMixin {
  TabController _tabController;
  int _selectedIndex = 0;
  PageController _pageController = PageController(initialPage: 0);
  var titles = [
    Ids.titleHome,
    Ids.titleExchange,
    Ids.titleLocation,
    Ids.titleQuestion,
    Ids.titleUser
  ];
  var icons = [
    IconFont.home,
    IconFont.ziyuanldpi,
    IconFont.lbs,
    IconFont.huabanfuben,
    IconFont.wode,
  ];
  var isPageCanChanged = true;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  void _onItemSelected(int index) {
    setState(() {
      _selectedIndex = index;
      onPageChange(_selectedIndex, p: _pageController);
    });
  }

  onPageChange(int index, {PageController p, TabController t}) async {
    if (p != null) {
      //判断是哪一个切换
      isPageCanChanged = false;
      _pageController.jumpToPage(index);
//      await _pageController.animateToPage(index,
//          duration: Duration(milliseconds: 400),
//          curve: Curves.ease); //等待pageview切换完毕,再释放pageivew监听
      isPageCanChanged = true;
    } else {
      setState(() {
        _selectedIndex = index;
      });
    }
  }

  void _onTelPressed(BuildContext context) {
    showDialog(
        context: context,
        builder: (_) => new AlertDialog(
                title: new Text(
                    IntlUtil.getString(context, Ids.titleCallWorldExchange)),
                content: new Text("400-118-9500"),
                actions: <Widget>[
                  new FlatButton(
                    child: new Text(
                      "Cancel",
                      style: TextStyle(color: Colors.red),
                    ),
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                  ),
                  new FlatButton(
                    child: new Text(
                      "Call",
                      style: TextStyle(color: Theme.of(context).primaryColor),
                    ),
                    onPressed: () {
                      Navigator.of(context).pop();
                      NavigatorUtil.launchInBrowser("tel:4001189500");
                    },
                  )
                ]));
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _tabController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    LogUtil.e("MainPagess build......");
    return new Scaffold(
      appBar: new AppBar(
        elevation: 0.5,
        backgroundColor: Colors.white,
        titleSpacing: 0.0,
        actions: <Widget>[
          IconButton(
            icon: Icon(IconFont.jisuan),
            onPressed: () {
              _onTelPressed(context);
            },
            color: ColorT.app_main,
          ),
          IconButton(
            icon: Icon(IconFont.dianhua),
            onPressed: () {
              _onTelPressed(context);
            },
            color: ColorT.app_main,
          )
        ],
        title: Padding(
          padding: EdgeInsets.only(left: 12, top: 12, bottom: 12, right: 8),
          child: Row(
            children: <Widget>[
              Padding(
                padding: EdgeInsets.only(right: 8),
                child: Image.asset(Utils.getImgPath('top_logo')),
              ),
              Text(
                IntlUtil.getString(context, Ids.titleAppName),
                style: TextStyle(
                    color: ColorT.app_main,
                    fontSize: ScreenUtil.getScaleSp(context, 14.0)),
              )
            ],
          ),
        ),
      ),
      body: PageView.builder(
        itemCount: titles.length,
        physics: NeverScrollableScrollPhysics(),
        onPageChanged: (index) {
          if (isPageCanChanged) {
            //由于pageview切换是会回调这个方法,又会触发切换tabbar的操作,所以定义一个flag,控制pageview的回调
            onPageChange(index);
          }
        },
        controller: _pageController,
        itemBuilder: (BuildContext context, int index) {
          switch (index) {
            case 0:
              return new HomePage();
              break;
            case 1:
              return new ExchangePage();
              break;
            case 2:
              return new LocationPage();
              break;
            case 3:
              return new QuestionPage();
              break;
            case 4:
              return new UserPage();
              break;
            default:
              return new HomePage();
          }
        },
      ),
      bottomNavigationBar: new BottomTabBar(
        items: <BottomTabBarItem>[
          BottomTabBarItem(
              icon: Icon(icons[0]),
              title: Text(IntlUtil.getString(context, titles[0]))),
          BottomTabBarItem(
              icon: Icon(icons[1]),
              title: Text(IntlUtil.getString(context, titles[1]))),
          BottomTabBarItem(
              icon: Icon(icons[2]),
              title: Text(IntlUtil.getString(context, titles[2]))),
          BottomTabBarItem(
              icon: Icon(icons[3]),
              title: Text(IntlUtil.getString(context, titles[3]))),
          BottomTabBarItem(
              icon: Icon(icons[4]),
              title: Text(IntlUtil.getString(context, titles[4]))),
        ],
        currentIndex: _selectedIndex,
        onTap: _onItemSelected,
        type: BottomTabBarType.fixed,
        isAnimation: false,
        isInkResponse: true,
        badgeColor: Colors.green,
      ),
    );
  }
}
